package com.module.circle.ui.medalDetails

import android.graphics.drawable.Drawable
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.common.app.data.bean.circle.CirclesMedalBadgesBean
import com.common.app.utls.TimeUtils
import com.common.base.app.extras.load
import com.common.base.app.extras.setVisibility
import com.common.base.app.fragment.BaseVMFragment
import com.davemorrissey.labs.subscaleview.ImageSource
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
import com.module.circle.R
import com.module.circle.databinding.CircleFragmentMedalDetailsBinding
import java.io.File


/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2025/08/14
 *
 * 描述：
 *
 * 修订历史：
 *
 */
class MedalDetailsFragment :
    BaseVMFragment<CircleFragmentMedalDetailsBinding, MedalDetailsViewModel>() {

    companion object {
        fun newInstance(mType: String, bean:CirclesMedalBadgesBean? =null): MedalDetailsFragment {
            val fragment = MedalDetailsFragment()
            fragment.mType = mType
            fragment.badge = bean
            return fragment
        }
    }

    private var mType: String = ""

    var badge:CirclesMedalBadgesBean? =null

//    override fun getPlaceholderView() = mViewBinding.placeView

    override fun getViewModel() = MedalDetailsViewModel::class.java

    override fun getViewBinding() = CircleFragmentMedalDetailsBinding.inflate(layoutInflater)

    override fun initViews() {
        super.initViews()
        badge?.let {
//            mViewBinding.imgMedal.load(it.active_url)

            //使用Glide下载图片,保存到本地
            Glide.with(this)
                .download(it.active_url)
                .into(object : CustomTarget<File?>() {
                    override fun onLoadFailed(errorDrawable: Drawable?) {
                        super.onLoadFailed(errorDrawable)
                    }

                    override fun onLoadCleared(placeholder: Drawable?) {
                    }

                    override fun onResourceReady(
                        resource: File,
                        transition: Transition<in File?>?
                    ) {
                        mViewBinding.imgMedal.setImage(
                            ImageSource.uri(
                                resource.absolutePath
                            )
                        )
                        mViewBinding.imgMedal.maxScale = 1f
                    }

                })
            mViewBinding.tvName.text = if (it.is_achieved) getString(R.string.common_have_obtained_hint) else getString(R.string.common_not_acquire_hint)
            setVisibility(mViewBinding.tvTime, it.achieved_at>0)
            mViewBinding.tvTime.text = TimeUtils.getYearMonthDay(it.achieved_at)
        }
    }

    override fun lazyInitData() {
        super.lazyInitData()
    }


    override fun initEvents() {
        super.initEvents()
        setErrorRetryListener {
            lazyInitData()
        }
    }
}